<?php
$options = array ();
$p = "";
$cs = Yii::app()->clientScript;
$cs->registerScriptFile(Yii::app()->baseUrl . '/js/masks.js', CClientScript::POS_HEAD);
$script = "var integerMask = new Mask(\"##\", \"number\");";
$script .= "var floatMask = new Mask(\"##.########################################\", \"number\");";
?>
<div class="yiiForm">

<?php


$form = $this->beginWidget('CActiveForm', array (
	'id' => 'edited-report-form',
	'enableAjaxValidation' => false,

	
));
?>

<?php $this->widget('application.components.RequiredMessage'); ?>

	<h2>Choix du document :</h2> 
	
	<?php


echo $form->hiddenField($model, 'edition_date');
echo $form->hiddenField($model, 'edition_user');
echo $form->hiddenField($model, 'filename');
echo $form->hiddenField($model, 'type');

echo $form->errorSummary($model);
?>

	<div class="simple">
		<?php echo $form->labelEx($model,'report_id'); ?>
		<?php


if ($model->report_id !== null && $model->report_id != "") {
	echo $form->hiddenField($model, 'report_id');
	echo $model->report->title;
} else {
	echo CHtml::activeDropDownList($model, 'report_id', CHtml::listData(Report::model()->findAll(), 'id', 'title'), array (
		'style' => 'width:250px;'
	));
}
?>
		<?php echo $form->error($model,'report_id'); ?>
	</div>

	<div class="simple">
		<?php echo $form->labelEx($model,'project_id'); ?>
		<?php


if ($model->project_id !== null && $model->project_id != "") {
	echo $form->hiddenField($model, 'project_id');
	echo $model->project->title;
} else {
	echo CHtml::activeDropDownList($model, 'project_id', CHtml::listData(Project::model()->findAll(), 'id', 'title'), array (
		'style' => 'width:250px;'
	));
}
?>
		<?php echo $form->error($model,'report_id'); ?>
	</div>


<?php
if ($model->report_id !== null && $model->report_id != "" && $model->project_id !== null && $model->project_id != "") {
	// Find report type parameter
	$p = Yii::app()->parameter->getParametersArray('RPTTYP');
	$p = $p->toAssociativeArray();
	
	if (isset ($p[$model->report->report_type])) {
		// Report is based on meeting
		if ($p[$model->report->report_type]["MTG_BASED"]) {
			echo '<div class="simple">';
			echo $form->labelEx($model,'meeting_id'); 
			if ($model->meeting_id !== null && $model->meeting_id != "") {
				echo $form->hiddenField($model, 'meeting_id');
				echo $model->meeting->meeting_date;
			} else {
				$filter = array (
					'project_id' => $model->project_id
				);
				if ($p[$model->report->report_type]["MTG_COMPL"])
					$filter['completed'] = true;
					
				echo CHtml::activeDropDownList($model, 'meeting_id', CHtml::listData(Meetings::model()->findAllByAttributes($filter), 'id', 'meeting_date_display'), array (
					'style' => 'width:250px;'
				));
			}
			echo $form->error($model,'meeting_id');
			echo '</div>'; 
		}
	}
}
	

if ($parameters != null && is_array($parameters) && count($parameters) > 0) {
?><br /><br />
	<h2>Paramètres du document :</h2> 
	<?php


	$i = 0;
	foreach ($parameters as $parameter) {
		echo CHtml::activeHiddenField($parameter, "[$i]name");
		echo CHtml::activeHiddenField($parameter, "[$i]label");
		echo CHtml::activeHiddenField($parameter, "[$i]type");
?>
<div class="simple">
<?php 
		$req = "";
		if (!$parameter->nullable) {
			$req = "<span class='required'>*</span>";
		}
		$p = "[$i]value";
		CHtml::resolveNameID($parameter, $p, $options);

		switch ($parameter->type) {
			case 'text' :
				echo "" . $parameter->label . "$req :";
				echo "<br /></div><div>";
				$this->widget('application.extensions.ckeditor.CKEditorWidget',array(
					"model"=>$parameter,
					"attribute"=>"[$i]value",
					"ckEditor"=>Yii::app()->basePath."/../ckeditor/ckeditor.php",
				    "ckBasePath"=>Yii::app()->baseUrl."/ckeditor/",
						"config" => array(
						"height"=>"200px",
						"width"=>"99%",
						"toolbar"=> Constants::ckEditorToolbars(),
					),
					"defaultValue" => $parameter->value,
				));
				
				/*echo CHtml::activeTextArea($parameter, "[$i]value", array (
					'cols' => 40,
					'rows' => 5
				));*/
				break;

			case 'float' :
			case 'integer' :
				echo "<label>" . $parameter->label . "$req :</label>";
				echo CHtml::activeTextField($parameter, $p);
				$script .= $parameter->type . 'Mask.attach($("#' . $options['id'] . '")[0]); ';
				break;

			case 'date' :
				echo "<label>" . $parameter->label . " :</label>";
				$this->widget('zii.widgets.jui.CJuiDatePicker', array (
					//'id'=>'param_value_'.$i,
					'model' => $parameter,
					'attribute' => "[$i]value",
					// additional javascript options for the date picker plugin
					'options' => Constants::$JuiDatePickerOptions
				));
				break;

			case 'boolean' :
				echo "<label>" . $parameter->label . "$req :</label>";
				echo CHtml::activeCheckBox($parameter, "[$i]value");
				break;

			case 'list' :
				echo "<label>" . $parameter->label . "$req :</label>";
				$values=$parameter->values;
				echo CHtml::activeDropDownList($parameter, "[$i]value", $values);
				break;

			default :
				echo "<label>" . $parameter->label . "$req :</label>";
				echo CHtml::activeTextField($parameter, "[$i]value");
				break;

		}
?></div><br /><?php


		$i++;
	}
?>
	</div>
	<?php


}
?>

	<div class="action">
		<?php echo CHtml::submitButton($model->isNewRecord ? Yii::t('views', 'Create') : Yii::t('views', 'Save'), array('class'=>'toto')); ?>
	</div>

<?php


$this->endWidget();

$cs->registerScript('topic_mask', $script);
?>

</div><!-- yiiForm -->